#include "GhostManager.h"

EnemyManager::EnemyManager()
{
	GameObject::GameObject();
}

EnemyManager::~EnemyManager()
{
	// do nothing
}

void EnemyManager::addGhostAI(int _id)
{
	GhostAI ghostName;
	if (_id == 1)
	{
		ghostName = eGhostPink;
	}
	mGhost.push_back(ghostName);
	iter = mGhost.begin();
	GhostAI ghostName1;
}

void EnemyManager::update(Point _PositionPacman, Vector2D _VelocityPacman, Point _PositionGhostRed)
{
	//std::list<GhostAI>::iterator iter = mGhost.begin();
	//std::list<GhostAI>::iterator iterNext = iter;
	for (std::list<GhostAI>::iterator iter = mGhost.begin(); iter != mGhost.end(); iter++)
	{
		//iterNext++;
		bool result = false;
		switch (*iter)
		{
		case eGhostPink:
			mDestination.x = _PositionPacman.x;
			mDestination.y = _PositionPacman.y;
			mPositionGhostPink.m_x = _VelocityPacman.m_x;
			mPositionGhostPink.m_y = _VelocityPacman.m_y;

			result = GhostPinkMove(mDestination);
			break;
		case eGhostBlue:

			mDestinationGhostBlue.x = (2 * _PositionPacman.x) -_PositionGhostRed.x;
			mDestinationGhostBlue.y = (2 * _PositionPacman.y) - _PositionGhostRed.y;
			result = GhostBlueMove(mDestinationGhostBlue);
			break;
		case eGhostYelow:

			mDestination.x = _PositionPacman.x;
			mDestination.y = _PositionPacman.y;
			//result = GhostYellowMove(mDestination);
			break;
		default:
			break;
		}
		//iter = iterNext;
	}
}

void EnemyManager::move(Point _PositionDestination)
{
	int ax = 4;
	int num = 3;
	int cross = 2;
	bool left = false;
	bool right = false;
	bool up = false;
	bool down = false;

	int ab = m_position.x;
	int ac = m_position.y;

	// direction right
	if (m_velocity.m_x == 1)
	{
		left = true;
		right = down = up = true;
		if (CheckUp(ab, ac) == 1)
		{
			up = false;
			num--;
			cross--;
		}
		if (CheckDown(ab, ac) == 1 && m_velocity.m_x == 1)
		{
			down = false;
			num--;
			cross--;
		}
		if (CheckRight(ab, ac) == 1 && m_velocity.m_x == 1)
		{
			right = false;
			num--;
			cross += 2;
		}
	}

	// direction left
	else if (m_velocity.m_x == -1)
	{
		right = true;
		left = down = up = true;
		if (CheckUp(ab, ac) == 1)
		{
			up = false;
			num--;
			cross--;
		}

		if (CheckDown(ab, ac) == 1 && m_velocity.m_x == -1)
		{
			down = false;
			num--;
			cross--;
		}
		if (CheckLeft(ab, ac) == 1 && m_velocity.m_x == -1)
		{
			left = false;
			num--;
			cross += 2;
		}
	}

	// direction down
	else if (m_velocity.m_y == 1)
	{
		up = true;
		right = left = down = true;
		if (CheckLeft(ab, ac) == 1)
		{
			left = false;
			num--;
			cross--;
		}
		if (CheckRight(ab, ac) == 1 && m_velocity.m_y == 1)
		{
			right = false;
			num--;
			cross--;
		}
		if (CheckDown(ab, ac) == 1 && m_velocity.m_y == 1)
		{
			down = false;
			num--;
			cross += 2;
		}
	}
	else if (m_velocity.m_y == -1)
	{
		down = true;
		right = left = up = true;
		if (CheckLeft(ab, ac) == 1)
		{
			left = false;
			num--;
			cross--;
		}
		if (CheckRight(ab, ac) == 1 && m_velocity.m_y == -1)
		{
			right = false;
			num--;
			cross--;
		}
		if (CheckUp(ab, ac) == 1 && m_velocity.m_y == -1)
		{
			up = false;
			num--;
			cross += 2;
		}
	}

	if (num > 1)
	{
		int x, y;

		x = m_position.x - _PositionDestination.x;
		y = m_position.y - _PositionDestination.y;
		if (x > y)
		{

			if (x > 0 && y > 0)
			{
				if (left == true)
				{
					m_velocity.m_x = -1;
					m_velocity.m_y = 0;
					//moveLeft();
				}
				else if (up == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = -1;
					// moveUp();
				}

			}
			else if (x < 0 && y < 0)
			{
				if (down == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = 1;
					// moveDown();
				}
				else if (right == true)
				{
					m_velocity.m_x = 1;
					m_velocity.m_y = 0;
					// moveRight();
				}
			}
			else if (x > 0 && y < 0)
			{
				if (left == true)
				{
					m_velocity.m_x = -1;
					m_velocity.m_y = 0;
					// moveLeft();
				}
				else if (up == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = -1;
					// moveUp();
				}

			}
		}
		// done

		else if (y > x)
		{
			if (x > 0 && y > 0)
			{
				/*m_velocity.m_x = 0;
				m_velocity.m_y = -1;*/
				if (up == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = -1;
					//moveUp();
				}
				else if (left == true)
				{
					m_velocity.m_x = -1;
					m_velocity.m_y = 0;
					// moveLeft();
				}

			}
			else if (x < 0 && y < 0)
			{
				/*m_velocity.m_x = 1;
				m_velocity.m_y = 0;*/
				if (right == true)
				{
					m_velocity.m_x = 1;
					m_velocity.m_y = 0;
					// moveRight();
				}
				else if (down == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = 1;
					// moveDown();
				}

			}
			else if (x < 0 && y > 0)
			{
				if (up == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = -1;
					// moveUp();
				}
				else if (right == true)
				{
					m_velocity.m_x = 1;
					m_velocity.m_y = 0;
					// moveRight();
				}

			}
		}
		if (x == 0 || y == 0)
		{
			if (x == 0)
			{
				if (y > 0 && up == true)
				{
					m_velocity.m_y = -1;
					m_velocity.m_x = 0;
				}
				else if (y > 0 && up == false)
				{
					m_velocity.m_x = 1;
					m_velocity.m_y = 0;
				}
				else if (y < 0 && down == true)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = 1;
				}
				else if (y < 0 && down == false)
				{
					m_velocity.m_x = 1;
					m_velocity.m_y = 0;
				}
			}
			else if (y == 0)
			{
				if (x > 0 && left == true)
				{
					m_velocity.m_x = -1;
					m_velocity.m_y = 0;
				}
				else if (x < 0 && right == true)
				{
					m_velocity.m_x = 1;
					m_velocity.m_y = 0;
				}
				else if (right == false)
				{
					m_velocity.m_x = 0;
					m_velocity.m_y = -1;
				}
			}
		}
	}
	else if (cross == 3)
	{
		if (m_velocity.m_y == -1)
		{
			if (right == true)
			{
				m_velocity.m_x = 1;
				m_velocity.m_y = 0;
			}
			else if (left == true)
			{
				m_velocity.m_x = -1;
				m_velocity.m_y = 0;
			}
		}
		else if (m_velocity.m_x == 1)
		{
			if (up == true)
			{
				m_velocity.m_x = 0;
				m_velocity.m_y = -1;
			}
			else if (down == true)
			{
				m_velocity.m_x = 0;
				m_velocity.m_y = 1;
			}
		}
		else if (m_velocity.m_x == -1)
		{
			if (up == true)
			{
				m_velocity.m_x = 0;
				m_velocity.m_y = -1;
			}
			else if (down == true)
			{
				m_velocity.m_x = 0;
				m_velocity.m_y = 1;
			}
		}
		else if (m_velocity.m_y == 1)
		{
			if (right == true)
			{
				m_velocity.m_x = 1;
				m_velocity.m_y = 0;
			}
			else if (left == true)
			{
				m_velocity.m_x = -1;
				m_velocity.m_y = 0;
			}
		}

		if (m_velocity.m_x == 0 && m_velocity.m_y == 1)
		{
			if (mAnimation == true)
			{
				m_idSprite = id_magic_sprite = 10;
			}
		}
		else if (m_velocity.m_x == 0 && m_velocity.m_y == -1)
		{

			if (mAnimation == true)
			{
				m_idSprite = id_magic_sprite = 14;
			}
		}
		else if (m_velocity.m_x == 1 && m_velocity.m_y == 0)
		{
			if (mAnimation == true)
			{
				m_idSprite = id_magic_sprite = 8;
			}
		}
		else if (m_velocity.m_x == -1 && m_velocity.m_y == 0)
		{
			if (mAnimation == true)
			{
				m_idSprite = id_magic_sprite = 12;
			};
		}

		//////////////////////////////////////////////////////////////
	}
	timeOfGame += dt;
	if (timeOfGame > timeDelay)
	{
		m_idSprite++;
		timeOfGame = 0;
		mAnimation = false;
		if (m_idSprite > id_magic_sprite + 1)
		{
			m_idSprite = id_magic_sprite;
			mAnimation = true;
		}
	}
	m_position.x = m_position.x + m_velocity.m_x;
	m_position.y = m_position.y + m_velocity.m_y;

	int acx = m_position.x;
	int acy = m_position.y;

	int w = 20;
	int h = 20;

	m_sprite.setDrawingCoordinates(acx, acy, w, h, m_idSprite);
	box.x = m_position.x;
	box.y = m_position.y;
}

bool EnemyManager::GhostPinkMove(Point _VelocityPacman)
{
	mDestination.x += 6 * _VelocityPacman.x;
	mDestination.y += 6 * _VelocityPacman.y;
	move(mDestination);
	return true;
}

bool EnemyManager::GhostBlueMove(Point _VelocityPacman)
{
	mDestination.x = _VelocityPacman.x;
	mDestination.y = _VelocityPacman.y;
	move(mDestination);
	return true;
}

void EnemyManager::creatGhostPink(int _idSprite)
{
	GhostAI ghostName;
	ghostName = eGhostPink;
	mGhost.push_back(ghostName);
}

void EnemyManager::creatGhostBlue(int _idSprite)
{

	GhostAI ghostName;
	ghostName = eGhostBlue;
	mGhost.push_back(ghostName);
}

void EnemyManager::creatGhostYellow(int _Sprite)
{
	GhostAI ghostName;
	ghostName = eGhostYelow;
	mGhost.push_back(ghostName);
}